const urlLocalStorageKey = 'newTab-key-89s0dfysahf82h394uh'
const iframeLocalStorageKey = 'newTab-iframe-89s0dfysahf82h394uh'
const url = window.localStorage.getItem(urlLocalStorageKey)

if (url === '空白页') {
  // 空白页
} else if (url) {
  if (window.localStorage.getItem(iframeLocalStorageKey) === 'true') { // 在iframe中打开url时
    // 创建 iframe
    let iframe = document.createElement('iframe')
    iframe.src = url || 'about:blank'
    document.body.appendChild(iframe);
    // 监听iframe中post的message事件，打开message中的url
    window.addEventListener('message', function (event) {
      window.open(event.data, '_self', 'noopener,noreferrer')
    })
  } else { // 直接打开url时
    document.location.href = url
  }
} else {
  document.getElementById('welcome').style.display = 'block'
}

// #regeion 搜索框
const searchBox = document.getElementById('search-icon');
const searchInput = document.getElementById('search-input');

searchBox.addEventListener('click', function () {
  searchInput.style.opacity = 1; // 渐显输入框
  searchInput.style.visibility = 'visible'
  searchInput.focus();
  searchBox.style.display = 'none'
});

// 输入框失去焦点时，渐隐输入框
searchInput.addEventListener('blur', function () {
  searchInput.style.visibility = 'hidden';
  searchInput.style.opacity = 0; // 渐隐输入框
  searchBox.style.display = 'block'
});
//#endregion
